package com.gwcloud.leetcode.interview100;

/** 
* @ClassName   : Leetcode0289 
* @author      : ganwei 
* @date        : 2020年5月29日 
* @Description : TODO 
*/
public class Leetcode0289 {
    /**
     * -解题思路
     */
	public void gameOfLife(int[][] board) {
		if(board.length == 0) {
			return;
		}
		int[][] newboard = new int[board.length][board[0].length];
		int m = board.length;
		int n = board[0].length;
		for(int i = 0; i < m; i++) {
			for(int j = 0; j < n; j++) {
				newboard[i][j] = getNext(board, i, j, m, n);
			}
		}
		
    }
	
	/**
	 * -getNext
	 */
	public int getNext(int[][] board, int i, int j, int m, int n) {
		int livecounts = 0;
		int deadcounts = 0;
		if(i+1>=0 && i+1<m && j+1>=0 && j+1<n) {
			if(board[i+1][j+1] == 1) {
				livecounts++;
			}else {
				deadcounts--;
			}
		}
		if(i+1>=0 && i+1<m && j>=0 && j<n) {
			if(board[i+1][j] == 1) {
				livecounts++;
			}else {
				deadcounts--;
			}
		}
		if(i+1>=0 && i+1<m && j-1>=0 && j-1<n) {
			if(board[i+1][j-1] == 1) {
				livecounts++;
			}else {
				deadcounts--;
			}
		}
		if(i>=0 && i<m && j-1>=0 && j-1<n) {
			if(board[i][j-1] == 1) {
				livecounts++;
			}else {
				deadcounts--;
			}
		}
		if(i>=0 && i<m && j+1>=0 && j+1<n) {
			if(board[i][j+1] == 1) {
				livecounts++;
			}else {
				deadcounts--;
			}
		}
		if(i-1>=0 && i-1<m && j+1>=0 && j+1<n) {
			if(board[i-1][j+1] == 1) {
				livecounts++;
			}else {
				deadcounts--;
			}
		}
		if(i-1>=0 && i-1<m && j>=0 && j<n) {
			if(board[i-1][j] == 1) {
				livecounts++;
			}else {
				deadcounts--;
			}
		}
		if(i-1>=0 && i-1<m && j-1>=0 && j-1<n) {
			if(board[i-1][j-1] == 1) {
				livecounts++;
			}else {
				deadcounts--;
			}
		}
		
		if(board[i][j] == 1) {
			if(livecounts < 2) {
				return 0;
			}else if(livecounts == 2) {
				return 1;
			}else if(livecounts == 3) {
				return 1;
			}else {
				return 0;
			}
		}
		
		if(board[i][j] == 0) {
			if(livecounts == 3) {
				return 1;
			}
		}
		return board[i][j];
	}
	
    /**
     * -主函数
     */
    public static void main(String[] args) {
    	
    }
}
